﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Apewer.Internals
{

    internal class TextHelper
    {

        /// <summary>获取文本的长度。</summary>
        public static int Len(string argOrigin)
        {
            return string.IsNullOrEmpty(argOrigin) ? 0 : argOrigin.Length;
        }

        /// <summary>将文本转换为小写。</summary>
        public static string LCase(string argOrigin)
        {
            return string.IsNullOrEmpty(argOrigin) ? "" : argOrigin.ToLower();
        }

        /// <summary>将文本转换为大写。</summary>
        public static string UCase(string argOrigin)
        {
            return string.IsNullOrEmpty(argOrigin) ? "" : argOrigin.ToLower();
        }

        /// <summary>获取文本中间的部分，起始位置从 0 开始。</summary>
        public static string Mid(string argParent, int argStart, int argLength)
        {
            return Middle(argParent, argStart, argLength);
        }

        /// <summary>获取文本的位置。</summary>
        public static int InStr(string argParent, string argSub)
        {
            if (string.IsNullOrEmpty(argParent)) return -1;
            if (string.IsNullOrEmpty(argSub)) return -1;
            return argParent.IndexOf(argSub);
        }

        /// <summary>获取文本中间的部分，起始位置从 0 开始。</summary>
        public static string Middle(string argParent, int argStart, int argLength)
        {
            if (string.IsNullOrEmpty(argParent)) return "";

            var vstart = argStart;
            if (vstart < 0) vstart = 0;
            if (vstart >= argParent.Length) return "";

            var vlength = argLength;
            if (vlength <= 0) return "";
            if ((vlength + vstart) > argParent.Length)
            {
                vlength = argParent.Length - vstart;
            }

            return argParent.Substring(vstart, vlength);
        }

        /// <summary>获取文本左边的部分。</summary>
        public static string Left(string argParent, int argLength)
        {
            return Middle(argParent, 0, argLength);
        }

        /// <summary>获取文本右边的部分。</summary>
        public static string Right(string argParent, int argLength)
        {
            if (string.IsNullOrEmpty(argParent)) return "";
            return Middle(argParent, argParent.Length - argLength, argLength);
        }

    }

}
